﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 跟踪弹
/// </summary>
public class BulletTracking : Bullet
{
    Transform target;//目标
    float rotSpeed = 2;//子弹转向速度

    Vector3 direction;

    public override void Fire(float speed)
    {
        this.speed = speed;

        //寻目标
        EnemyHP enemy = RoleManager.Instance.GetOneEnemy();
        target = enemy.transform;

        GameObject.Destroy(gameObject, 5f);
    }

	void Update () {
        if (target)//跟踪目标
        {
            direction = target.position - transform.position;
            direction.z = 0;
            direction = Vector3.Normalize(direction);

            //转向目标
            transform.up = Vector3.MoveTowards(transform.up, direction, rotSpeed * Time.deltaTime);
        }
        //飞行
        transform.Translate(Vector3.up * speed * Time.deltaTime);
    }

}
